<template>
    <el-form label-width="80px" v-loading="loading">
        <el-form-item label="登录账号:">
            <el-input type="text" v-model="user.account" />
        </el-form-item>
        <el-form-item label="真实姓名:">
            <el-input type="text" v-model="user.name" />
        </el-form-item>
        <el-form-item label="登录密码:">
            <el-input type="text" v-model="user.password" show-password/>
        </el-form-item>
        <el-form-item label="确认密码:">
            <el-input type="text" v-model="user.cpassword" show-password/>
        </el-form-item>
        <el-form-item label="所属职位:">
            <el-select v-model="user.roleId" style="width: 100%" @change="roleChange">
                <el-option label="请选择" value=""></el-option>
                <template v-for="(value,index) of roleData" :key="index">
                    <el-option  :label="value.roleName" :value="value.roleId" v-if="value.roleId!==1"></el-option>
                </template>
            </el-select>
        </el-form-item>
        <el-form-item label="所属科室:" v-if="user.roleId===3">
            <el-select v-model="user.deptId" style="width: 100%">
                <el-option label="请选择" value="0"></el-option>
                <el-option v-for="(value,index) of deptData" :key="index" :label="value.deptName" :value="value.deptId"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="确认操作:">
            <el-button style="width: 100%" @click="addUser">
                确认修改
            </el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import utils from "@/Utils";
    export default {
        name: "SystemPopupAddUser",
        data(){
            return{
                user:{
                    account:'',
                    name:'',
                    password:'',
                    cpassword:'',
                    roleId:'',
                    deptId:"0"
                },
                roleData:[],
                deptData:[],
                loading:false,
                utils
            }
        },
        methods:{
            LoadDeptDatas:async function(){
                this.loadingSelect=true
                await this.axios.get("/Dept/isLogin/getDepts").then((req)=>{
                    const data=req.data;
                    if(data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return;
                    }
                    this.deptData=data.data;
                    this.loadingSelect=false;
                })
            },
            LoadRoleDatas:async function(){
                await this.axios.get("/Role/isLogin/getRoles").then((req)=>{
                    const data=req.data;
                    if(data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return;
                    }
                    this.roleData=data.data;
                })
            },
            roleChange:function (val) {
                if (val!=="3") this.user.deptId="0"
            },
            addUser:function () {
                const user=this.user;
                if (this.utils.isEmpty(user.account, user.deptId, user.cpassword, user.name, user.password, user.roleId)) {
                    this.utils.tipsMessage("请不要留空或不选",1);
                    return
                }
                if (user.password !== user.cpassword) {
                    this.utils.tipsMessage("请确认密码是一致...",1);
                    return
                }
                this.loading=true
                this.axios.post("/User/isLogin/isSystemAdmin/addUser",{
                    account:user.account,
                    name:user.name,
                    password:user.password,
                    roleId:user.roleId,
                    deptId:user.deptId
                }).then((req)=>{
                    const data=req.data;
                    if(data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return
                    }
                    this.utils.tipsMessage(data.data,data.code);
                    this.user={
                        account:'',
                        name:'',
                        password:'',
                        cpassword:'',
                        roleId:'',
                        deptId:"0"
                    }
                }).then(()=>{
                    this.loading=false
                })
            }
        },
        mounted(){
            this.LoadDeptDatas();
            this.LoadRoleDatas();
        }
    }
</script>

<style scoped>

</style>